from fastapi import APIRouter, HTTPException, BackgroundTasks
from app.models.order import Order, Side
from app.services.twap import twap_worker      # 下面实现
from app.services.iceberg import iceberg_worker
from app.services.broker import dry_run        # 先 Dry-Run

router = APIRouter(prefix="/api/order")

@router.post("/send", response_model=Order)
def send_order(order: Order, background: BackgroundTasks):
    if order.qty <= 0:
        raise HTTPException(status_code=400, detail="qty <= 0")
    # 1. 落库
    dry_run.save(order)
    # 2. 异步 TWAP 任务    
    if order.algo == 'iceberg':
        background.add_task(iceberg_worker, order.id)
    else:
        background.add_task(twap_worker, order.id)
    return order
